package com.example.androidmainshixun.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.entity.Goods;
import com.example.androidmainshixun.view.Goods1Activity;
import com.example.androidmainshixun.view.Goods2Activity;
import com.example.androidmainshixun.view.Goods3Activity;
import com.example.androidmainshixun.view.Goods4Activity;
import com.example.androidmainshixun.view.Goods5Activity;

import java.util.List;

public class MyBaseAdapter extends BaseAdapter {
    private String[] titles;
    private String[] prices;
    private  String[] places;
    private  int[] icons;
    private Context context;
    // 在 MyBaseAdapter 中添加一个 Goods List
    private List<Goods> goodsList;

    public MyBaseAdapter(Context context, String[] titles, String[] prices, String[] places, int[] icons, List<Goods> goodsList) {
        this.context = context;
        this.titles = titles;
        this.prices = prices;
        this.places = places;
        this.icons = icons;
        // 确保所有数组在使用前都已经被初始化
        if (titles == null || prices == null || icons == null) {
            throw new IllegalArgumentException("Titles, prices, and icons arrays must not be null");
        }
    }
    @Override
    public int getCount(){       //得到item的总数
        return titles.length;    //返回ListView Item条目代表的对象
    }

    @Override
    public Object getItem(int position){
        return titles[position]; //返回item的数据对象
    }
    @Override
    public long getItemId(int position){ return position;}//返回item的id

    @Override
    public View getView(int position, View convertView, ViewGroup parent){//获取item中的View视图
        ViewHolder holder;
        if(convertView==null){
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
            holder=new ViewHolder();
            holder.title=convertView.findViewById(R.id.title);
            holder.price=convertView.findViewById(R.id.price);
            holder.place=convertView.findViewById(R.id.place);
            holder.iv=convertView.findViewById(R.id.iv);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }


        // 为ImageView设置数据和点击事件监听器
        ImageView iconImageView = holder.iv;
        iconImageView.setImageResource(icons[position]);
        iconImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击事件处理
                // 根据position确定意图启动的Activity
                Intent intent;
                switch (position) {
                    case 0:
                        intent = new Intent(context, Goods1Activity.class);
                        break;
                    case 1:
                        intent = new Intent(context, Goods2Activity.class);
                        break;
                    case 2:
                        intent = new Intent(context, Goods3Activity.class);
                        break;
                    case 3:
                        intent = new Intent(context, Goods4Activity.class);
                        break;
                    case 4:
                        intent = new Intent(context, Goods5Activity.class);
                        break;
                    default:
                        throw new IllegalArgumentException("Invalid position: " + position);
                }
                // 传递点击项的数据
                intent.putExtra("POSITION", position);
                // 启动相应的Activity
                context.startActivity(intent);
            }
        });


        // 确保 position 在有效范围内
        if (position >= 0 && position < titles.length) {
            holder.title.setText(titles[position]);
            holder.price.setText(prices[position]);
            holder.place.setText(places[position]); // 确保 places 数组也在使用前被初始化
            holder.iv.setImageResource(icons[position]);
        } else {
            // 处理无效的 position，例如通过设置默认值或抛出异常
        }

        return convertView;
    }






    class ViewHolder{
        TextView title;
        TextView price;
        TextView place;
        ImageView iv;
    }
}


